package com.easou.ps.master.knowledge;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.easou.ps.exec.Executor;
import com.easou.ps.exec.ExecutorBuilder;
import com.easou.ps.master.kit.ConfigFactory;
import com.easou.ps.master.search.KSTask;
import com.easou.ps.master.search.SearchTarget;
import com.easou.ps.thrift.ks.KnowledgeSearchRequest;
import com.easou.ps.thrift.ks.KnowledgeSearchResponse;
import com.easou.ps.thrift.qs.QSResult;
import com.easou.ps.thrift.search.types.SearchScope;
import com.easou.ps.thrift.search.types.WorkerType;
import com.easou.ps.webmaster.SearchRequest;

public class RequestKnowledge {
	private static final Logger LOG = Logger.getLogger("search");

	static final Executor<KnowledgeSearchResponse> searchExecutor = new ExecutorBuilder<KnowledgeSearchResponse>("knowledgeSearch")
				.setCoreSize(ConfigFactory.getSysConfig().getInt("knowledgeSearch.threads", 100)).setMaxSize(ConfigFactory.getSysConfig().getInt("knowledgeSearch.max.threads", 100)).setIdleTime(1)
				.setQueueSize(10000).setTimeout(ConfigFactory.getSysConfig().getInt("knowledgeSearch.timeout", 1000)).toExecutor();
	private long timeCost = 0L;
	private KSTask task = null;
	
	public long getTimeCost() {
		return timeCost;
	}

	public static Executor<KnowledgeSearchResponse> getSearchExecutor() {
		return searchExecutor;
	}

	public boolean toGet(SearchRequest request, QSResult qsResult) {
		searchExecutor.prepare();
		boolean searchKS = ConfigFactory.getAppConfig().getBool("knowledge.switch", true);
		if (!searchKS) {
			return false;
		}
		if (request.getPageNum() > 1) {
			return false;
		}
		try {
			KnowledgeSearchRequest req = changeRequest(request,qsResult);
			if(req == null){
				return false;
			}
			SearchTarget target = new SearchTarget(WorkerType.Hourly, SearchScope.TITLE);
			Map<String, String> workerInfo = new HashMap<String, String>();
			KnowledgeSearchRequest workerReq = req.deepCopy();
			task = new KSTask(workerReq, target, workerInfo);
			searchExecutor.execTask(task);
			return true;
		} catch (Exception e) {
			LOG.error("com.easou.ps.master.knowledge.RequestKnowledge.toGet error : " + e);
			return false;
		}
	}
	
	public List<KnowledgeSearchResponse> toGetResults(){
		List<KnowledgeSearchResponse> results = searchExecutor.getResults();
		timeCost = task.getTimeCost();
		return results;
	}

	private static KnowledgeSearchRequest changeRequest(SearchRequest request, QSResult qsResult) {
		KnowledgeSearchRequest knowledgeSearchRequest = null;
		if (qsResult != null && qsResult.getKsQuery() != null) {
			knowledgeSearchRequest = new KnowledgeSearchRequest();
			knowledgeSearchRequest.setQueryStr(request.getQ());
			knowledgeSearchRequest.setSearchFlag(1 + 4 + 8 + 16 + 32 + 64);
			knowledgeSearchRequest.setTermInfoList(qsResult.getKsQuery().getTermInfoList());
			knowledgeSearchRequest.setSynTermInfoList(qsResult.getKsQuery().getSynTermInfoList());
			knowledgeSearchRequest.setTitleLength(25);
			knowledgeSearchRequest.setSummaryLength(80);
			knowledgeSearchRequest.setDebug(false);
			knowledgeSearchRequest.setViewVersion(request.getViewVersion());
		}
		return knowledgeSearchRequest;
	}
}
